// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// i [author] rhyme_lph
// g [email]  rhymelph@gmail.com
// R [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/entities/address_entity.dart';
import 'package:finance_app/src/entities/country_entity.dart';
import 'package:finance_app/src/http_service/http_common_service.dart';
import 'package:finance_app/src/widgets/bottom_sheet/address_bottom_sheet.dart';
import 'package:finance_app/src/widgets/bottom_sheet/country_bottom_sheet.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/page/hr_payroll/employee/views/employee_other_view.dart';

class EmployeeOtherProvider extends BaseProvider {
  TextEditingController phoneNumberController = TextEditingController();
  FocusNode phoneNumberFocusNode = FocusNode();
  TextEditingController contactPhoneNumberController = TextEditingController();
  FocusNode contactPhoneNumberFocusNode = FocusNode();

  TextEditingController unitNumberController = TextEditingController();
  FocusNode unitNumberFocusNode = FocusNode();


  String countryCode = '+65';
  String contactCountryCode = '+65';

  CountryEntity country;

  String address;
  TextEditingController postalCodeController = TextEditingController();
  FocusNode postalCodeFocusNode = FocusNode();

  TextEditingController contactNameController = TextEditingController();
  FocusNode contactNameFocusNode = FocusNode();

  TextEditingController relationshipController = TextEditingController();
  FocusNode relationshipFocusNode = FocusNode();

  TextEditingController bankController = TextEditingController();
  FocusNode bankFocusNode = FocusNode();

  TextEditingController bankCodeController = TextEditingController();
  FocusNode bankCodeFocusNode = FocusNode();

  TextEditingController accountHolderNameController = TextEditingController();
  FocusNode accountHolderNameFocusNode = FocusNode();

  TextEditingController accountNumberController = TextEditingController();
  FocusNode accountNumberFocusNode = FocusNode();

  TextEditingController branchCodeController = TextEditingController();
  FocusNode branchCodeFocusNode = FocusNode();

  void updateCountryCode(value) {
    countryCode = value;
    update();
  }

  @override
  void dispose() {
    super.dispose();
    phoneNumberController.dispose();
    phoneNumberFocusNode.dispose();
    contactPhoneNumberController.dispose();
    contactPhoneNumberFocusNode.dispose();
    unitNumberController.dispose();
    unitNumberFocusNode.dispose();
    postalCodeController.dispose();
    postalCodeFocusNode.dispose();
    contactNameController.dispose();
    contactNameFocusNode.dispose();
    relationshipController.dispose();
    relationshipFocusNode.dispose();
    bankController.dispose();
    bankFocusNode.dispose();
    bankCodeController.dispose();
    bankCodeFocusNode.dispose();
    accountHolderNameController.dispose();
    accountHolderNameFocusNode.dispose();
    accountNumberController.dispose();
    accountNumberFocusNode.dispose();
    branchCodeController.dispose();
    branchCodeFocusNode.dispose();
  }

  @override
  Future get dataFuture => null;

  void onChangeCountry() {
    obtainContext?.call((context) async {
      final _country = await showCountryBottomSheet(
        context,
        country,
      );
      if (_country != null) {
        country = _country;
        update();
      }
    });
  }

  void unFocus() {
    obtainContext?.call((context) {
      FocusScope.of(context).requestFocus(FocusNode());
    });
  }

  void onChangeAddress() {
    unFocus();
    obtainContext?.call((context) async {
      AddressEntity entity = await showAddressBottomSheet(context, null,
          (String searchData) async {
        List<AddressEntity> addressList;
        await post(HttpCommonService.searchAddress(searchData),
            autoHandler: AutoHandler.Toast, onSuccess: (result) {
          addressList = getEntityFromResult<List<AddressEntity>>(result);
        });
        if (addressList == null) {
          addressList = [];
        }
        return addressList;
      });
      if (entity != null) {
        address = entity.address;
        postalCodeController.text = entity.code;
        update();
      }
    });
  }
}
